<?php

namespace Xrfg\Core\Common\Services;

class Proof
{
    /**
     * @param $proof_id |凭证id
     * @param $class_id |凭证模型编号
     * @param $op_id |操作类型编号
     *
     * @return string
     */
    public static function encode($proof_id, $class_id, $op_id)
    {
        $class_id = str_pad($class_id, 3, '0', STR_PAD_LEFT);
        $op_id = str_pad($op_id, 2, '0', STR_PAD_LEFT);

        return $proof_id.$class_id.$op_id;
    }

    /**
     * @param $no  |凭证号
     *
     * @return array [记录id,凭证模型编号, 操作类型编号]
     */
    public static function decode($no)
    {
        $op_id = (int) substr($no, -2, 2);
        $class_id = (int) substr($no, -5, 3);
        $proof_id = substr($no, 0, -5);

        return [
            $proof_id,
            $class_id,
            $op_id,
        ];
    }

    public static function getOpName($certificate)
    {
        $arr = self::decode($certificate);

        return config("finance.op_code.{$arr[1]}.{$arr[2]}.name");
    }
}
